﻿var dbquota = 500;
var stgquota = 500;
var bwquota = 10000;
var quotaExceedingPercent = .55;
var maxGaugeValue = 30000;
        
$(document).ready(function () {
    renderChart(['bwi', 'bwe'], 'chartMesage1', 'chart1');
    renderChart(['dbi', 'dbe'], 'chartMesage2', 'chart2');
    renderChart(['dbs'], 'chartMesage2', 'chart3');
    renderChart(['stg'], 'chartMesage2', 'chart4');

    $.template("topDbTemplate", $('#topDbTemplate'));

    var url = new cloudninja.metering.services.UrlBuilder()
        .forEntity("MonthlyView")
        .query(cloudninja.metering.services.Settings.serviceUrlPath)
        .withMonth(month)
        .withYear(year)
        .orderBy("Value desc")
        .takeTop(10);

    var dbTopUrl = url.withMeterType("dbs").build();
    $.getJSON(dbTopUrl, function (results) {
        $("#topdblist").empty();
        $.tmpl("topDbTemplate", results.value, { Quota: dbquota }).appendTo("#topdblist");
        $(".topTenantsContainer table").trigger("update");
    });

    var stgTopUrl = url.withMeterType("stg").build();
    $.getJSON(stgTopUrl, function (results) {
        $("#topstglist").empty();
        $.tmpl("topDbTemplate", results.value, { Quota: stgquota }).appendTo("#topstglist");
        $(".topTenantsContainer table").trigger("update");
    });

    var bweTopUrl = url.withMeterType("bwe").build();
    $.getJSON(bweTopUrl, function (results) {
        $("#topbwelist").empty();
        $.tmpl("topDbTemplate", results.value, { Quota: bwquota }).appendTo("#topbwelist");
        $(".topTenantsContainer table").trigger("update");
    });

    var baseGaugeUrl = new cloudninja.metering.services.UrlBuilder()
        .forEntity("TotalMonthlyView")
        .query(cloudninja.metering.services.Settings.serviceUrlPath)
        .withMonth(month)
        .withYear(year)
        .takeTop(1);

    var dbsGaugeUrl = baseGaugeUrl.withMeterType("dbs").build();
    var stgGaugeUrl = baseGaugeUrl.withMeterType("stg").build();
    var bweGaugeUrl = baseGaugeUrl.withMeterType("bwe").build();

    var gaugeRenderer = new cloudninja.metering.charting.GaugeRenderer(maxGaugeValue, 80);

    cloudninja.metering.services.GenericService.loadSingleResult(dbsGaugeUrl, function (result, data) {
        gaugeRenderer.render('gauge1', 'KB', data.Value);
    });

    cloudninja.metering.services.GenericService.loadSingleResult(stgGaugeUrl, function (result, data) {
        gaugeRenderer.render('gauge2', 'KB', data.Value);
    });

    cloudninja.metering.services.GenericService.loadSingleResult(bweGaugeUrl, function (result, data) {
        gaugeRenderer.render('gauge3', 'KB', data.Value);
    });

    $.tablesorter.addParser({
        id: "slashDivided",
        is: function (data) {
            return false;
        },
        format: function (data) {
            return data.toString().split("/")[0];
        },
        type: "numeric"
    });

    $(".topTenantsContainer table").tablesorter({ headers: { 1: { sorter: "slashDivided"}} });
});

function renderChart(meterTypes, label, containerId) {
    $(label).text("fetching...");

    var startDateTime = new Date(year, month - 1, 1, 0, 0, 0);
    var endDateTime = new Date(new Date(startDateTime).setMonth(startDateTime.getMonth() + 1));

    cloudninja.metering.services.GenericService.getTotalDailyStats(meterTypes, startDateTime, endDateTime, function (result, data) {
        if (data.length == 0) {
            $(label).text("no data");
            return;
        }

        $(label).text("");

        var series = cloudninja.metering.services.GenericService.prepareDailySeriesData(data, meterTypes);
        cloudninja.metering.charting.ChartRenderer.renderAxes(containerId, series);
    });
}